const Koa = require('koa');

const app = new Koa();

const path = require('path');

//ctx.request.body
const bodyparser = require('koa-bodyparser');

//1.处理静态资源

const static = require('koa-static');

app.use(static(path.join(__dirname,'./public/')));

const Router = require('koa-router');

const router = new Router();

const query = require('./db/query');

app.use(bodyparser()); //参数 ctx.request.body

//2.路由

app.use(router.routes());

app.use(router.allowedMethods());



//获取所有成员信息
//分页  pagenum 当前的页数  limit 2
router.get('/api/userlist',async (ctx,next) => {
    //查询list表所有数据 

    let {pagenum=1,limit=2} = ctx.query;
    //limit 起始下标,每页的条数
    let startIndex = (pagenum-1)*limit;
    let data = await query(`select * from list limit ${startIndex},${limit}`);
    ctx.body = {
        code:1,
        data
    };
})

//搜索
router.get('/api/search',async (ctx,next) => {
    let {key} = ctx.query;
    if(key){
        let data = await query(`select * from list where username like '%${key}%'`);
        ctx.body = {
            code:1,
            data
        }
    }else{
        ctx.body = {
            code:2,
            msg:'搜索内容为空'
        }
    }
})

//添加成员信息
router.post('/api/add',async (ctx,next) => {
    //如何接收前端post传递数据
    console.log(ctx.request.body);
    let {username,age,sex,phone,address,id_card} = ctx.request.body;
    if(username && age && sex && phone && address && id_card){
        try{
            await query('insert into list (username,age,sex,phone,address,id_card) values (?,?,?,?,?,?)',[username,age,sex,phone,address,id_card]);
            ctx.body = {
                code:1,
                msg:"添加成功"
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数有误'
        }
    }
})

//更新
router.put('/api/update',async (ctx,next) => {
    let {username,age,sex,phone,address,id_card,id} = ctx.request.body;
    if(username && age && sex && phone && address && id_card && id){
        try{
            //尝试着去更新
            await query('update list set username=?,age=?,sex=?,phone=?,address=?,id_card=? where id=?',[username,age,sex,phone,address,id_card,id]);
            ctx.body = {
                code:1,
                msg:'修改成功'
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数有误'
        }
    }
})

//删除
router.delete('/api/del',async (ctx,next) => {
    //删除的是谁 id
    let {id} = ctx.query;
    if(id){
        try{
            await query('delete from list where id=?',[id]);
            ctx.body = {
                code:1,
                msg:'删除成功'
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数有误'
        }
    }
})

app.listen(3000,() => {
    console.log('服务启动成功')
})